﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace YouDaoNotesCodes
{
    class JsonAnalysis
    {
        /// <summary>
        /// 本地JASON解析;参考：https://www.cnblogs.com/hbgjh/p/9176773.html（注意编码格式，在此基础上做了修改）
        /// </summary>
        public void readJSONMethod1()
        {
            ArrayList list = new ArrayList();//动态数据存放JSON解析后的值
            string jsonfile = "E://jsonFile.json";
            using (StreamReader file = new StreamReader(jsonfile, Encoding.Default))//注意编码，StreamReader会出现读取中文乱码问题
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject o = (JObject)JToken.ReadFrom(reader);
                    string a = o["lotname"].ToString();
                    var b = o["other"];
                    var c = b["lotaddress"];
                    var d = o["devices"];
                    foreach (JObject e in d)
                    {
                        var deviceID = e["deviceID"];
                        var name = e["name"];
                        var IP = e["IP"];
                        PubCommon.WriteLog(name.ToString(), System.Reflection.MethodBase.GetCurrentMethod().Name);
                        string str = deviceID.ToString() + "," + name.ToString() + "," + IP.ToString() + " ";
                        list.Add(str);
                    }
                }
            }
            foreach (string st in list)
            {
                Console.WriteLine("{0},", st);
            }
            Console.ReadKey();
        }
        /// <summary>
        /// 调用第三方类库newtonsoft.json 解析json（常用）
        /// </summary>
        public void readJSONMethod2()
        {
            string JSON = "{\"name\":\"Alan Walker\",\"sex\":\"M\",\"age\":\"20\"}";//JSON对象
            JObject jo = (JObject)JsonConvert.DeserializeObject(JSON);//反序列化
            string name = jo["name"].ToString();
            string sex = jo["sex"].ToString();
            string age = jo["age"].ToString();
            string info = name + "," + sex + "," + age + " ";
            ArrayList list = new ArrayList();
            list.Add(info);
            foreach (string str in list)
            {
                Console.Write(str);
            }
            Console.ReadKey();
        }
        /// <summary>
        /// 项目上用过的JSON解析方法
        /// </summary>
        public void readJSONMethod3()
        {
            string JSONDATA = "{'info': [{'name': 'Jay Chou','age':'35'},{'name': 'Alan Walker','age':'21'},{'name':'Taylor Swift','age':'26'}]}";//JSON数组
            string name = string.Empty;
            string age = string.Empty;
            ArrayList list = new ArrayList();
            DataSet ds = new DataSet();
            JavaScriptSerializer js = new JavaScriptSerializer();//这样也可以
            // JObject JO = JObject.Parse(JSONDATA);
            ds = JsonConvert.DeserializeObject<DataSet>(JSONDATA);
            DataTable dt = ds.Tables[0];
            //方式1
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                name = dr["name"].ToString();
                age = dr["age"].ToString();
                string str = name + "," + age;
                list.Add(str);
            }
            //方式2
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //注意下以下方法：假如我这么写-->
                name = ds.Tables[0].Rows[i][0].ToString();
                age = ds.Tables[0].Rows[i][1].ToString();
                string str = name + "," + age;
                list.Add(str);
            }

            foreach (string st in list)
            {
                Console.WriteLine(st);
            }
            Console.ReadKey();
            
        }
        /// <summary>
        /// Json生成实体类，类似于DataSet方式
        /// </summary>
        public void readJSONMethod4()
        {
            string JSONDATA = "{'info': [{'name': 'Jay Chou','age':'35'},{'name': 'Alan Walker','age':'21'},{'name':'Taylor Swift','age':'26'}]}";//JSON数组
            JsonAna ja = new JsonAna();
            ja = JsonConvert.DeserializeObject<JsonAna>(JSONDATA);
            foreach (InfoItem it in ja.info)
            {
                Console.WriteLine(it.name);
                Console.WriteLine(it.age);
            }
            Console.ReadKey();
        }
    }
    public class InfoItem
    {
        /// <summary>
        /// name
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// age
        /// </summary>
        public string age { get; set; }
    }

    public class JsonAna
    {
        /// <summary>
        /// 
        /// </summary>
        public List<InfoItem> info { get; set; }
    }
}
